package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.User;
import com.pdsu.xpf.graduation01.service.impl.UserServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.JwtComponent;
import com.pdsu.xpf.graduation01.utils.Result;
import com.pdsu.xpf.graduation01.utils.ShiroUtil;
import com.pdsu.xpf.graduation01.vo.OrderVO;
import com.pdsu.xpf.graduation01.vo.UserVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("user")
public class UserControl {

    private  long expTime= 1000*60*60*24*365;

    final
    JsonFormat jsonFormat;
    final
    ShiroUtil shiroUtil;
    final
    JwtComponent jwtComponent;
    final
    UserServiceImpl userService;

    public UserControl(UserServiceImpl userService, JwtComponent jwtComponent, ShiroUtil shiroUtil, JsonFormat jsonFormat) {
        this.userService = userService;
        this.jwtComponent = jwtComponent;
        this.shiroUtil = shiroUtil;
        this.jsonFormat = jsonFormat;
    }

    @ResponseBody
    @RequestMapping("/login")
    public Result userLogin(@Param("Username") String Username, @Param("Password") String Password ) throws JsonProcessingException {


        System.out.println(Username);
        System.out.println(Password);

        //密码和用户名 加密并转成16进制
        String password_md5=shiroUtil.md5(Username,Password);

        //检查账号和密码
         User ul= userService.findUserByUsernameAPassword(Username,password_md5);
         if (ul!=null){
             //信息匹配 生成新token 有效期为365天

             String token_temp= jwtComponent.makeToken(ul,expTime);
             ul.setToken(token_temp);
             //给user设置token属性，传入前端
             String user_j=JsonFormat.ObjToJson(ul);
             System.out.println(user_j);

             /**
              * 更新token
              */
             userService.upDateUserTokenByUserID(token_temp,ul.getUid());
             userService.updateUserDATEBYUID(ul.getUid());

             return Result.success(user_j);

         }else {
             //账号或密码错误
            return Result.failed();
         }

    }


    @ResponseBody
    @RequestMapping("/logincheck")
    public Result userLoginCheck(@Param("token") String token, @Param("phone")String phone,@Param("uid")String uid){
        System.out.println("token"+token);
        User u=userService.findUserByUID(Integer.parseInt(uid));
        u.setPhone(phone);
        System.out.println(phone+"phone");
        //生成新token并返回

        if ( jwtComponent.checkToken(token,u)){
           String new_token =  jwtComponent.makeToken(u,expTime);
           userService.upDateUserTokenByUserID(new_token,Integer.parseInt(uid));
           userService.updateUserDATEBYUID(Integer.parseInt(uid));
            return Result.success(new_token);
        }
       else {

            return Result.failed();
        }


    }

    @ResponseBody
    @RequestMapping("/registerUser")
    public Result registerUser(HttpServletRequest request) throws JsonProcessingException {
        //GET
        String json_user=request.getParameter("user");
        User user =JsonFormat.JsonToObj(json_user,User.class);

        System.out.println(user);

        //检查用户名
        User use=userService.findUserByUsername(user.getUsername());
        if (use==null){

            //在数据库中添加数据
                //1.给密码加密  2.生成token 3.存入数据库
           String pass_md5 = shiroUtil.md5(user.getUsername(),user.getPassword());

           user.setPassword(pass_md5);
            String token_temp=jwtComponent.makeToken(user,expTime);

            System.out.println(token_temp.length());
            System.out.println(token_temp.length());
            System.out.println(token_temp.length());
            user.setToken(token_temp);

            userService.insertUser(user);

            return Result.success(token_temp);

        }else {
            //如果存在 则注册不成功
            return Result.failed();
        }


    }

    @ResponseBody
    @RequestMapping("/findAllUser")
    public String findAllUser(){
        List<User> users = userService.findAllUser();
        for (User u:users){
            System.out.println(u.toString());
        }
        return users.get(0).toString();
    }

//addFeedBack

    @ResponseBody
    @RequestMapping("/addFeedBack")
    public Result addFeedBack(HttpServletRequest request){
        String fname = request.getParameter("fname");
        String msg = request.getParameter("msg");
        String uid = request.getParameter("uid");
        String phone = request.getParameter("phone");
        SimpleDateFormat sm= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date= sm.format(new Date());
        userService.addFeedBack(fname,msg,uid,phone,date);
        return Result.success();
    }

}
